package by.ad.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.memcache.Stats;

@SuppressWarnings("serial")
public class TechStatServlet
  extends HttpServlet
{
  @Override
  public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
    throws IOException, ServletException
  {
    resp.setCharacterEncoding("win-1251");
    resp.setContentType("text/plain");
    resp.setStatus(200);
    resp.setHeader("Expires", "Fri, 19 Feb 2010 16:13:37 GMT");
    resp.setHeader("Cache-Control", "public, must-revalidate, proxy-revalidate, max-age=0");
    
    final ServletOutputStream out = resp.getOutputStream();
    
    out.println(new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date()));
    out.println();
    
    final Stats stats = MemcacheServiceFactory.getMemcacheService().getStatistics();
    
    out.println("Memcache:");
    out.println(String.format("  ItemCount = %,d - Number of entries currently \"alive\" in the cache.", 
      stats.getItemCount()));  
    out.println(String.format("  HitCount  = %,d - The counter of \"successful\" MemcacheService.get(Object) or MemcacheService.contains(Object) operations.",
      stats.getHitCount()));  
    out.println(String.format("  MissCount = %,d - The counter of \"unsuccessful\" MemcacheService.get(Object) or MemcacheService.contains(Object) operations.", 
      stats.getMissCount()));  
    out.println(String.format("  BytesReturnedForHits = %,d - Sum of key and value bytes in successful get() and contains().",
      stats.getBytesReturnedForHits()));  
    out.println(String.format("  TotalItemBytes = %,d - Sum of key and value sizes for all live entries currently in cache.", 
      stats.getTotalItemBytes()));  
    out.println(String.format("  MaxTimeWithoutAccess = %,d - Seconds since last access of least-recently-used live entry.", 
      stats.getMaxTimeWithoutAccess()));
  }
  
}
